using System;
using System.Collections.Generic;

namespace Jukebox
{
    public abstract class State
    {
        protected State mNextState;
        public State ()
        {
            mNextState = this;
        }
        public virtual State Continue()
        {
            return mNextState;
        }
        public virtual void Start() {}
        public virtual void Stop() {}
        public virtual void AddSong(string song) {}
    }
    
    public class Stopped : State
    {
        public Stopped() {}
        public override void Start()
        {
            mNextState = new Idle();
        }
        public override string ToString()
        {
            return base.ToString() + ("[Stopped]");
        }
    }
    
    public class Idle : State
    {
        public Idle() {}
        public override void Stop()
        {
            mNextState = new Stopped();
        }
        public override void AddSong(string song)
        {
            mNextState = new Playing(song);
        }
        public override string ToString()
        {
            return base.ToString() + ("[Idle]");
        }
    }
    
    public class Playing : State
    {
        List<string> mSongs = new List<string>();
        DateTime mSongDone;
        public Playing(string song)
        {
            mSongs.Add(song);
            mSongDone = DateTime.Now + new TimeSpan(0, 3, 0);
        }
        public override void Stop()
        {
            mNextState = new Stopped();
        }
        public override void AddSong(string song)
        {
            mSongs.Add(song);
        }
        public override State Continue()
        {
            if( mSongDone <= DateTime.Now )
            { // Song is done
                mSongs.RemoveAt(0);
                if( mSongs.Count > 0 )
                { // Start next song
                    mSongDone = DateTime.Now+new TimeSpan(0, 3, 0);
                }
                else
                {
                    mNextState = new Idle();
                }
            }
            return base.Continue();
        }
        public override string ToString()
        {
            string str = "[Playing]";
            foreach( string s in mSongs )
            {
                str = String.Format("{0} - s", str, s);
            }
            return base.ToString() + str;
        }
    }
}

